/**
 * 贪心算法改进版，不用创建那么多变量
 * @param {number[]} prices
 * @return {number}
 */
const maxProfit = prices => {

  let max = sum = 0

  for (let i = 0; i < prices.length - 1; i++) {
    sum += (prices[i + 1] - prices[i])
    if (sum < 0) sum = 0
    max = Math.max(sum, max)
  }
  return max
}


console.log(maxProfit([7, 1, 5, 3, 6, 4]))